<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 作业1 累加器
        function sum(){
            var num = 0;
            function jia(){
                num++;
                console.log(num);
            }
            return jia;
        }

        var leijia = sum();
        leijia();
        leijia();

        // 作业2 班级学生名单
        function student(){
            var studentArr = [];
            var studentOperation = {
                enter: function(enterName){
                    studentArr.push(enterName);
                    console.log(studentArr);
                },
                leave: function(leaveName){
                    // studentArr.forEach(function(item, index) {
                    //     if(item == leaveName) {
                    //         studentArr.splice(index, 1);
                    //         return;
                    //     }
                    // });
                    var idx = studentArr.indexOf(leaveName);
                    if(idx != -1){
                        studentArr.splice(idx, 1);
                    }
                    console.log(studentArr);
                }
            };
            return studentOperation;
        }
        var stuOperation = student();
        stuOperation.enter('小明');
        stuOperation.enter('小猪');
        stuOperation.leave('小明');
    </script>
</body>
</html>